/*
* @Author: mmall
* @Date:   2017-05-27 17:57:49
* @Last Modified by:   Rosen
* @Last Modified time: 2017-05-28 19:48:16
*/

'use strict';
require('./index.css');
var _mm = require('util/mm.js');
var _product = require('service/product-service.js');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var echarts = require('page/common/echarts.min.js');
var _product = require('service/product-service.js');
var Pagination = require('util/pagination/index.js');
var templateIndex = require('./index.string');
var page = {
    data: {
        listParam: {
            // categoryId      : _mm.getUrlParam('categoryId') || '',
            maxTime: _mm.getUrlParam('maxTime') || '',
            minTime: _mm.getUrlParam('minTime') || '',
            current: _mm.getUrlParam('pageNum') || 1,
            pageSize: _mm.getUrlParam('pageSize') || 10,
        }
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        this.loadList();
    },
    bindEvent: function () {
        var _this = this;
        // 排序的点击事件
        $('#but_time').click(function () {
            var minTime = $("#minTime").val()
            var maxTime = $("#maxTime").val()
            _this.data.listParam.minTime = minTime
            _this.data.listParam.maxTime = maxTime
            // 重新加载列表
            _this.loadList();
        });
    },
    // 加载list数据
    loadList: function () {
        var _this = this,
            listHtml = '',
            listParam = this.data.listParam,
            $pListCon = $('.p-list-con');

        $pListCon.html('<div class="loading"></div>');
        // 删除参数中不必要的字段
        listParam.categoryId
            ? (delete listParam.keyword) : (delete listParam.categoryId);
        // 请求接口
        _product.getSearchDateHistogramList(listParam, function (res) {
            if (brandList){
                return;
            }
            var brandList = res;
            var myChart = echarts.init(document.getElementById('main'));
            var brandNames = [];
            var brandValues = [];
            brandList.forEach(function (item) {
                brandNames.push(item["brand"]);
                brandValues.push(item["sumValue"]);
            })
        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '根据日期统计销量'
            },
            tooltip: {},
            legend: {
                data: ['销量']
            },
            xAxis: {
                data: brandNames
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'bar',
                data: brandValues,
                itemStyle: {
                    normal: {
                        label: {
                            show: true,		//开启显示
                            position: 'top',	//在上方显示
                            textStyle: {	    //数值样式
                                color: 'black',
                                fontSize: 16
                            }
                        }
                    }
                }
            }]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
        console.log(res.dataMap)
    }, function(errMsg) {
        _mm.errorTips(errMsg);
    }
)
;
},
// 加载分页信息
loadPagination: function (pageInfo) {
    var _this = this;
    this.pagination ? '' : (this.pagination = new Pagination());
    this.pagination.render($.extend({}, pageInfo, {
        container: $('.pagination'),
        onSelectPage: function (pageNum) {
            _this.data.listParam.current = pageNum;
            _this.loadList();
        }
    }));
}
}
;
$(function () {
    page.init();
})